package com.stars.easyms.swagger.plugin;

import com.stars.easyms.rest.annotation.EasyMsRestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import springfox.documentation.service.ResolvedMethodParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationModelsProviderPlugin;
import springfox.documentation.spi.service.contexts.RequestMappingContext;

import java.util.List;

/**
 * <p>className: EasyMsOperationModelsProvider</p>
 * <p>description: EasyMs的swagger的Models提供者类</p>
 *
 * @author guoguifang
 * @version 1.2.2
 * @date 2019-07-30 18:41
 */
public final class EasyMsOperationModelsProvider implements OperationModelsProviderPlugin {

    @Override
    public void apply(RequestMappingContext context) {
        if (context.findAnnotation(EasyMsRestMapping.class).isPresent()) {
            List<ResolvedMethodParameter> parameterTypes = context.getParameters();
            if (parameterTypes.size() != 1) {
                return;
            }
            ResolvedMethodParameter parameterType = parameterTypes.get(0);
            if (!parameterType.hasParameterAnnotation(RequestBody.class)
                    && !parameterType.hasParameterAnnotation(RequestPart.class)) {
                context.operationModelsBuilder().addInputParam(context.alternateFor(parameterType.getParameterType()));
            }
        }
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return true;
    }
}